<?php
// ------------------------------------------------------------------------- 
//	program_schedule
//		Copyright 2005, PichardDesign
// 		http://www.picharddesign.com
// ------------------------------------------------------------------------- 
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //
require_once "../../mainfile.php";
include XOOPS_ROOT_PATH.'/header.php';
echo "<LINK rel='stylesheet' type='text/css' media='all'  href=\"program_schedule.css\" />\n";

include_once(XOOPS_ROOT_PATH."/class/module.textsanitizer.php");
$myts =& MyTextSanitizer::getInstance();




//get default view as set in admin
if(!isset($_SESSION['view'])){
	$result = $xoopsDB->query( "SELECT view, DailyViewCurrent, announcements, start_time, letters FROM ".$xoopsDB->prefix("program_schedule_configuration")."" ) ;
		if ( $result ) {
			$myrow = $xoopsDB->fetchArray($result);
			$_SESSION['view'] = $myrow['view'];
			$_SESSION['showCurrentProgram'] = $myrow['DailyViewCurrent'];
			$_SESSION['announcements'] = $myrow['announcements'];
			$_SESSION['start_time'] = $myrow['start_time'];
			$_SESSION['letters'] = $myrow['letters'];
			
		}
}




//get category listings
	 $categories = $xoopsDB->query( "SELECT * FROM ".$xoopsDB->prefix("program_schedule_categories")."" ) ;
	 $catData = array();
	 		while ( $mycat = $xoopsDB->fetchArray($categories) ) {
				$catData[] = $mycat;
			//++$i;
			}
			
			
	function findCategory($ID){
	global $catData;
		for ($i = 0; $i < count($catData); $i++) {
			if(array_search ($ID, $catData[$i])){
				return $catData[$i]['color'];
				break;
			}
		}
		
	}	



//#########################################################
//#########################################################
//#########################################################

if(isset($_GET['view'])){
	$_SESSION['view'] = $_GET['view']; 
}

//list out Daily view
if($_SESSION['view'] == 'day'){
	$dayViewData = array();
	$foundStart = "";
	$result = $xoopsDB->query( "SELECT * FROM ".$xoopsDB->prefix("program_schedule_programs")." WHERE ".strtolower(date("D"))." = 'true' ORDER BY start_time asc" ) ;
		if ($result){
			$x=0;
			while ($myrow = $xoopsDB->fetchArray($result)){
				$dayViewData[] = $myrow;
				if($myrow['end_time'] > mktime(date("H"),date("i"),date("s"),0,0,0) && $foundStart != 'true'){
					$foundStart = 'true';
					$arrayStart = $x;
				}
			++$x;
			}
		}
		
		if(!isset($arrayStart)){
			$arrayStart = 0;
		}
		
		
		
		
		
		if($_SESSION['showCurrentProgram'] == 'true'){//splice array to show only current and upcoming programs
			$pastData = array_slice ($dayViewData, 0, $arrayStart);   
			$dayViewData = array_slice ($dayViewData, $arrayStart);
		}
		
		
		
	echo("<table width=\"100%\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\" >");
	echo("<tr><td valign=\"top\" align=\"left\"><table width='100%' ><tr> <td><span style='color:#666666; padding-left:5px; padding-bottom:5px; padding-top:5px; font-weight:600; font-size:17px;'>Program Guide</span></td> <td  width='25' align='right'><img src='images/print.gif' style='border:0' /></td> <td align='right' width='145' nowrap><span style='padding-right:5px; padding-bottom:5px; padding-top:5px; font-size:11px;'><a href='print.php' target='_blank'>printer friendly grid</a></span> </td> </tr></table></td></tr>");
	echo("<tr><td valign=\"top\" align=\"left\" style='padding-top:10px; padding-bottom:5px; padding-left:5px; font-size:15px; font-weight:600; color:#666666'>".date("l, F j, Y")." &nbsp;<span style='padding-right:5px; padding-bottom:5px; padding-top:5px; font-size:11px;'><a href='index.php?view=week'>View Full Week</a></span></td></tr>");
	
	//Display Announcements if any
	if ($_SESSION['announcements'] != "") {
	include_once(XOOPS_ROOT_PATH."/class/module.textsanitizer.php");
	$myts =& MyTextSanitizer::getInstance();
			echo " <tr><td style='padding-left:5px; padding-bottom:5px;'><span style='font-weight:600;'>*NOTE: </span>";
			echo $myts->displayTarea($_SESSION['announcements']);
			echo"</td></tr><P></P>";
	}
	
	
	
	//Display Legend
	echo"<tr><td valign=\"top\" align=\"left\" style='padding-left:5px; padding-right:5px; padding-bottom:5px;'><div style='padding-bottom:5px; padding-top:5px; padding-left:5px; border: 1px solid #999999;'><span style='font-weight:600; font-size:11px; '>Program Legend:</span> &nbsp; ";
		$width = 100/count($catData);
		for ($i = 0; $i < count($catData); $i++) {
			echo "<span style='padding:5px; width:75px; height:25px; background-color:#".$catData[$i]['color']."; border: 1px solid #999999;'>".$catData[$i]['description']."</span> &nbsp; &nbsp;";
		}
		
	echo"</div></td></tr>";
	
	
	
	//create row data
	for ($i = 0; $i < count($dayViewData); $i++) {
		if($dayViewData[$i]['link'] != ""){//is there a valid link?
			echo"<tr class='headerLink'><th><table><tr><td width='150'>".date("h:i A",$dayViewData[$i]['start_time'])."</td> <td><a href='".$dayViewData[$i]['link']."' TARGET='_blank'>".stripslashes ($dayViewData[$i]['title'])."</a></td></tr></table></th></tr>";
		}else{
			echo"<tr class='headerLink'><th><table><tr><td width='150'>".date("h:i A",$dayViewData[$i]['start_time'])."</td> <td>".stripslashes ($dayViewData[$i]['title'])."</td></tr></table></th></tr>";
		}
		
		//get image if there is one
		if($dayViewData[$i]['image']){
			$image = "<img src = 'images/uploads/".$dayViewData[$i]['image']."' style='border:1px solid #000000'>";
		}else{
			$image = "";
		}
		
		if($dayViewData[$i]['DJ']){
			$DJ = $dayViewData[$i]['DJ']."<br />";
		}else{
			$DJ = "";
		}
		
		$description = stripslashes ( $myts->makeTareaData4Show($dayViewData[$i]['description']) );
		if($description == ""){
			$description = '&nbsp;';
		}
		
		echo "<tr bgcolor='#".findCategory($dayViewData[$i]['category'])."'><td> <table><tr> <td width='154' align='center' valign='top'>$image</td> <td valign='top'><span style='font-weight:600;'>".$DJ."</span>".$description."</td> </table> </td></tr>";
		
	}
	
	
 	//display stuff from earlier in the day if configed to do so... 
	if($pastData){
	echo"<tr><td style='border: 1px solid #999999; font-weight:600; font-size:15px; padding-top:3px; padding-bottom:3px; text-align:center; background-color:#333333; color:#FFFFFF'>From Earlier Today:</td</tr>";
		//create row data
		for ($i = 0; $i < count($pastData); $i++) {
			if($pastData[$i]['link'] != ""){//is there a valid link?
				echo"<tr class='headerLink'><th><table><tr><td width='150'>".date("h:i A",$pastData[$i]['start_time'])."</td> <td><a href='".$pastData[$i]['link']."' TARGET='_blank'>".stripslashes ($pastData[$i]['title'])."</a></td></tr></table></th></tr>";
			}else{
				echo"<tr class='headerLink'><th><table><tr><td width='150'>".date("h:i A",$pastData[$i]['start_time'])."</td> <td>".stripslashes ($pastData[$i]['title'])."</td></tr></table></th></tr>";
			}
			
			//get image if there is one
			if($pastData[$i]['image']){
				$image = "<img src = 'images/uploads/".$pastData[$i]['image']."' style='border:1px solid #000000'>";
			}else{
				$image = "";
			}
			
			if($pastData[$i]['DJ']){
				$DJ = $pastData[$i]['DJ']."<br />";
			}else{
				$DJ = "";
			}
			
		$description = stripslashes ( $myts->makeTareaData4Show($pastData[$i]['description']) );
		if($description == ""){
			$description = '&nbsp;';
		}
		
			
			echo "<tr bgcolor='#".findCategory($pastData[$i]['category'])."'><td> <table><tr> <td width='154' align='center' valign='middle'>$image</td> <td valign='top'><span style='font-weight:600;'>".$DJ."</span>".$description."</td> </table> </td></tr>";
			
		}
	}
	
	
	
	echo("</table>");
	
	
	
	
//#########################################################
//#########################################################
//######################################################### 

}else{//diplay weekly view
	$weekViewData = array();
	$result = $xoopsDB->query( "SELECT * FROM ".$xoopsDB->prefix("program_schedule_programs")." WHERE start_time >= '".$_SESSION['start_time']."' ORDER BY start_time asc, mon DESC, tue DESC, wed DESC, thu DESC, fri DESC, sat DESC, sun DESC" ) ;
		if ($result){
			$x=0;
			while ($myrow = $xoopsDB->fetchArray($result)){
				$weekViewData[] = $myrow;
			++$x;
			}
		}
		

	echo("<table width=\"100%\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\">");
	echo("<tr><td valign=\"top\" align=\"left\"><table width='100%'><tr> <td><span style='color:#666666; padding-left:5px; padding-bottom:5px; padding-top:5px; font-weight:600; font-size:17px;'>Program Guide</span></td> <td  width='25'><img src='images/print.gif'  style='border:0' /></td> <td align='right' width='145'><span style='padding-right:5px; padding-bottom:5px; padding-top:5px; font-size:11px;'><a href='print.php' target='_blank'>printer friendly grid</a></span> </td> </tr></table></td></tr>");
	echo("<tr><td valign=\"top\" align=\"left\" style='padding-top:10px; padding-bottom:5px; padding-left:5px; font-size:15px; font-weight:600; color:#666666'>".date("l, F j, Y")." &nbsp;<span style='padding-right:5px; padding-bottom:5px; padding-top:5px; font-size:11px;'><a href='index.php?view=day'>View Today Only</a></span></td></tr>");
	
	//Display Announcements if any
	if ($_SESSION['announcements'] != "") {
	include_once(XOOPS_ROOT_PATH."/class/module.textsanitizer.php");
	$myts =& MyTextSanitizer::getInstance();
			echo " <tr><td style='padding-left:5px; padding-bottom:5px;'><span style='font-weight:600;'>*NOTE: </span>";
			echo $myts->displayTarea($_SESSION['announcements']);
			echo"</td></tr><P></P>";
	}
	
	
	 
	//Display Legend
	echo"<tr><td valign=\"top\" align=\"left\" style='padding-left:5px; padding-right:5px; padding-bottom:5px;'><div style='padding-bottom:5px; padding-top:5px; padding-left:5px; border: 1px solid #999999;'><span style='font-weight:600; font-size:11px; '>Program Legend:</span> &nbsp; ";
		$width = 100/count($catData);
		for ($i = 0; $i < count($catData); $i++) {
			echo "<span style='padding:5px; width:75px; height:25px; background-color:#".$catData[$i]['color']."; border: 1px solid #999999;'>".$catData[$i]['description']."</span> &nbsp; &nbsp;";
		}
		
	echo"</div></td></tr>\n";
	
	
	//Conjur Weekly View
	echo "<tr><td><table width='100%' cellspacing='0' cellpadding='0'>\n";
	
	//Column Headers
	echo "<tr class='columnHeaderYo'><td width='9%' nowrap>&nbsp;</td><td width='12%'>Monday</td><td width='12%'>Tuesday</td><td width='12%'>Wednesday</td><td width='12%'>Thursday</td><td width='12%'>Friday</td><td width='12%'>Saturday</td><td width='12%'>Sunday</td>	</tr>\n\n";
	
	
	
	
		//figure out columns and rows
		$arrayPointer = 0;
		function getRows($loopCounter,$minutes){
		global $arrayPointer;
		global $weekViewData;
		$days = array('mon','tue','wed','thu','fri','sat','sun');
		
		//find out start time of program and loop row
		$advancePointer = "";
		$currenrRowData = array();//temp array for all entries in this row
		for ($i = $arrayPointer; $i < count($weekViewData); $i++) {
			if(date("h:i a",$weekViewData[$arrayPointer]['start_time']) == date("h:i a",mktime($loopCounter,$minutes,00,09,02,1973))){
				$currenrRowData[] = $weekViewData[$arrayPointer];//get all progam entries for curent row
				++$arrayPointer;
			}else{
				break;
			}
		}
		
		
		//print_r($currenrRowData);
		
		
		//Figure out number of dyas/cells for each entry
		$rowString = "";
		for ($i = 0; $i < count($currenrRowData); $i++) {
		$activeDays = array();
		$non_activeDays = array();
		
			for ($r = 0; $r <= (7); $r++) {
				//echo $r."<BR>";
				if($r != 7 && $currenrRowData[$i][$days[$r]] == 'true'){
					$activeDays[] = $days[$r];//number of days prgram has
				}else{//build cell
					$rowSpan = ($currenrRowData[$i]['end_time'] - $currenrRowData[$i]['start_time'])/1800;
					if(count($activeDays) > '0' && $currenrRowData[$i]['link'] != ""){
						$rowString = $rowString . "<td class='weekDisplay' bgcolor='#".findCategory($currenrRowData[$i]['category'])."' colspan='".count($activeDays)."' rowspan='$rowSpan'><a href='".$currenrRowData[$i]['link']."' TARGET='_blank'>".$currenrRowData[$i]['title'] ."</a></td>";
					}elseif(count($activeDays) > '0' && $currenrRowData[$i]['link'] == ""){
						$rowString = $rowString . "<td class='weekDisplay' bgcolor='#".findCategory($currenrRowData[$i]['category'])."' colspan='".count($activeDays)."' rowspan='$rowSpan'>".$currenrRowData[$i]['title'] ."</td>";
					}
				$activeDays = array();//reset array
				}
			}//end days loop
			
			
		}//end loop


	echo $rowString;
		
	}//end function
	
	
	
	$AM_set = 'false';
	$PM_set = 'false';
	

	//Loop through and build the rows...
	for ($i = 0; $i < 24; $i++) {
	
			if(mktime($i,0,0,0,0,0) >= $_SESSION['start_time']){
				if($AM_set != 'true' && date("a",mktime($i,0,0,0,0,0)) == 'am'){
					echo"<tr><td class='timeColumn' nowrap style='font-weight:600'>".date("h:ia",mktime($i,0,0,0,0,0))."</td>";
					$AM_set = 'true';
				}elseif($PM_set != 'true' && date("a",mktime($i,0,0,0,0,0)) == 'pm'){
					echo"<tr><td class='timeColumn' nowrap  style='font-weight:600'>".date("h:ia",mktime($i,0,0,0,0,0))."</td>";
					$PM_set = 'true';
				}else{
					echo"<tr><td class='timeColumn' nowrap>".date("h:i",mktime($i,0,0,0,0,0))."</td>";
				}
				
				getRows($i,00);
			echo"</tr>\n";
			}
			
			//for 30 minute block
			if(mktime($i,30,0,0,0,0) >= $_SESSION['start_time']){
			echo"<tr><td class='timeColumn' nowrap>".date("h:i",mktime($i,30,0,0,0,0))."</td>";
				getRows($i,30);
			echo"</tr>\n";
			}
	}




//#################################################
//####### BEGIN DATA FROM BEFORE SESSION START TIME
//#################################################


if(date("g:i:s a",$_SESSION['start_time']) != '12:00:00 am'){

	$weekViewData = array();

	 $result = $xoopsDB->query( "SELECT * FROM ".$xoopsDB->prefix("program_schedule_programs")." WHERE start_time < '".$_SESSION['start_time']."' ORDER BY start_time asc, mon DESC, tue DESC, wed DESC, thu DESC, fri DESC, sat DESC, sun DESC" ) ;
		if ($result){
			$x=0;
			while ($myrow = $xoopsDB->fetchArray($result)){
				$weekViewData[] = $myrow;
			++$x;
			}
		} 
	
	
	
	if(count($weekViewData) != '0'){
		echo"<tr><td style='border: 1px solid #999999; font-weight:600; font-size:15px; padding-top:3px; padding-bottom:3px; text-align:center; background-color:#333333; color:#FFFFFF' colspan='8'>From The Morning Hours:</td></tr>";
		$AM_set = 'false';
		$PM_set = 'false';
		
		$arrayPointer = 0;
		//Loop through and build the rows...
		for ($i = 0; $i < 24; $i++) {
		
				if(mktime($i,0,0,0,0,0) < $_SESSION['start_time']){
					if($AM_set != 'true' && date("a",mktime($i,0,0,0,0,0)) == 'am'){
						echo"<tr><td class='timeColumn' nowrap style='font-weight:600'>".date("h:i a",mktime($i,0,0,0,0,0))."</td>";
						$AM_set = 'true';
					}elseif($PM_set != 'true' && date("a",mktime($i,0,0,0,0,0)) == 'pm'){
						echo"<tr><td class='timeColumn' nowrap  style='font-weight:600'>".date("h:i a",mktime($i,0,0,0,0,0))."</td>";
						$PM_set = 'true';
					}else{
						echo"<tr><td class='timeColumn' nowrap>".date("h:i",mktime($i,0,0,0,0,0))."</td>";
					}
					
					getRows($i,00);
				echo"</tr>\n";
				}
				
				//for 30 minute block
				if(mktime($i,30,0,0,0,0) < $_SESSION['start_time']){
				echo"<tr><td class='timeColumn' nowrap>".date("h:i",mktime($i,30,0,0,0,0))."</td>";
					getRows($i,30);
				echo"</tr>\n";
				}
			
			
	}
			
			
			
			
	}		
		
		
		
		
		
		
		

}
		echo"</table></td></tr>";//close table
	echo("</table>");
}

include XOOPS_ROOT_PATH."/footer.php";
?>